home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 November: Tool Chest / Dev.CD Nov 00 TC Disk 2.toast / pc / sample code / quicktime / quicktimeintro / wiredsprites / application files / comapplication.c next >
Encoding:
Text File  |  2000-10-06  |  14.3 KB  |  567 lines

  1. //////////
  2. //
  3. //    File:        ComApplication.c
  4. //
  5. //    Contains:    Application-specific code for basic QuickTime movie display and control.
  6. //
  7. //    Written by:    Tim Monroe
  8. //                Based on the QTShell code written by Tim Monroe, which in turn was based on the MovieShell
  9. //                code written by Kent Sandvik (Apple DTS). This current version is now very far removed from
  10. //                MovieShell.
  11. //
  12. //    Copyright:    © 1999 by Apple Computer, Inc., all rights reserved.
  13. //
  14. //    Change History (most recent first):
  15. //       
  16. //       <2>         03/20/00    rtm        made changes to get things running under CarbonLib
  17. //       <1>         11/05/99    rtm        first file
  18. //
  19. //////////
  20.  
  21. //////////
  22. //
  23. // header files
  24. //
  25. //////////
  26.  
  27. #include "ComApplication.h"
  28. #include "QTWiredSprites.h"
  29.  
  30.  
  31. //////////
  32. //
  33. // global variables
  34. //
  35. //////////
  36.  
  37. #if TARGET_OS_MAC
  38. AEEventHandlerUPP        gHandleOpenAppAEUPP;                    // UPPs for our Apple event handlers
  39. AEEventHandlerUPP        gHandleOpenDocAEUPP;
  40. AEEventHandlerUPP        gHandlePrintDocAEUPP;
  41. AEEventHandlerUPP        gHandleQuitAppAEUPP;
  42. extern Boolean            gAppInForeground;                        // is our application in the foreground?    
  43. #endif
  44.  
  45. #if TARGET_OS_WIN32
  46. extern HWND                ghWnd;                                    // the MDI frame window; this window has the menu bar
  47. #endif
  48.  
  49.  
  50. //////////
  51. //
  52. // QTApp_Init
  53. // Do any application-specific initialization.
  54. //
  55. // The theStartPhase parameter determines which "phase" of application start-up is executed,
  56. // *before* the MDI frame window is created or *after*. This distinction is relevant only on
  57. // Windows, so on MacOS, you should always use kInitAppPhase_BothPhases.
  58. //
  59. //////////
  60.  
  61. void QTApp_Init (UInt32 theStartPhase)
  62. {
  63.     // do any start-up activities that should occur before the MDI frame window is created
  64.     if (theStartPhase & kInitAppPhase_BeforeCreateFrameWindow) {
  65.     
  66.         // check to make sure that QuickTime wired sprites are available;
  67.         // we depend on these features  
  68.         if (!QTUtils_HasWiredSprites())
  69.             QTFrame_QuitFramework();
  70.  
  71. #if TARGET_OS_MAC
  72.         // make sure that the Apple Event Manager is available; install handlers for required Apple events
  73.         QTApp_InstallAppleEventHandlers();
  74. #endif
  75.     }
  76.  
  77.     // do any start-up activities that should occur after the MDI frame window is created
  78.     if (theStartPhase & kInitAppPhase_AfterCreateFrameWindow) {
  79. #if TARGET_OS_WIN32
  80.         // on Windows, open as movie documents any files specified on the command line
  81.         SendMessage(ghWnd, WM_OPENDROPPEDFILES, 0L, 0L);
  82. #endif
  83.     }
  84. }
  85.  
  86.  
  87. //////////
  88. //
  89. // QTApp_Stop
  90. // Do any application-specific shut-down.
  91. //
  92. // The theStopPhase parameter determines which "phase" of application shut-down is executed,
  93. // *before* any open movie windows are destroyed or *after*.
  94. //
  95. //////////
  96.  
  97. void QTApp_Stop (UInt32 theStopPhase)
  98. {    
  99.     // do any shut-down activities that should occur before the movie windows are destroyed
  100.     if (theStopPhase & kStopAppPhase_BeforeDestroyWindows) {
  101.         
  102.     }
  103.     
  104.     // do any shut-down activities that should occur after the movie windows are destroyed
  105.     if (theStopPhase & kStopAppPhase_AfterDestroyWindows) {
  106. #if TARGET_OS_MAC
  107.         // dispose of routine descriptors for Apple event handlers
  108.         DisposeAEEventHandlerUPP(gHandleOpenAppAEUPP);
  109.         DisposeAEEventHandlerUPP(gHandleOpenDocAEUPP);
  110.         DisposeAEEventHandlerUPP(gHandlePrintDocAEUPP);
  111.         DisposeAEEventHandlerUPP(gHandleQuitAppAEUPP);
  112. #endif
  113.     
  114.     }
  115. }
  116.  
  117.  
  118. //////////
  119. //
  120. // QTApp_Idle
  121. // Do any processing that can/should occur at idle time.
  122. //
  123. //////////
  124.  
  125. void QTApp_Idle (WindowReference theWindow)
  126. {
  127.     WindowObject         myWindowObject = NULL;
  128.     GrafPtr             mySavedPort;
  129.     //Cursor                myArrow;
  130.     
  131.     GetPort(&mySavedPort);
  132.     MacSetPort(QTFrame_GetPortFromWindowReference(theWindow));
  133.     
  134.     myWindowObject = QTFrame_GetWindowObjectFromWindow(theWindow);
  135.     if (myWindowObject != NULL) {
  136.         MovieController        myMC = NULL;
  137.     
  138.         myMC = (**myWindowObject).fController;
  139.         if (myMC != NULL) {
  140.             
  141.             // run any idle-time tasks for the movie
  142.             
  143. #if TARGET_OS_MAC
  144.             // restore the cursor to the arrow
  145.             // if it's outside the front movie window or outside the window's visible region
  146.             if (theWindow == QTFrame_GetFrontMovieWindow()) {
  147.                 Rect            myRect;
  148.                 Point            myPoint;
  149.                 RgnHandle        myVisRegion;
  150.                 Cursor            myArrow;
  151.                 
  152.                 GetMouse(&myPoint);
  153.                 myVisRegion = NewRgn();
  154.                 GetPortVisibleRegion(QTFrame_GetPortFromWindowReference(theWindow), myVisRegion);
  155.                 GetWindowPortBounds(theWindow, &myRect);
  156.                 if (!MacPtInRect(myPoint, &myRect) || !PtInRgn(myPoint, myVisRegion))
  157.                     MacSetCursor(GetQDGlobalsArrow(&myArrow));
  158.                     
  159.                 DisposeRgn(myVisRegion);
  160.             }
  161. #endif // TARGET_OS_MAC
  162.         }
  163.     }
  164.     
  165.     // ***insert application-specific idle-time processing here***
  166.     
  167.     MacSetPort(mySavedPort);
  168. }
  169.  
  170.  
  171. //////////
  172. //
  173. // QTApp_Draw
  174. // Update the specified window.
  175. //
  176. //////////
  177.  
  178. void QTApp_Draw (WindowReference theWindow, Rect *theRefreshArea)
  179. {
  180. #pragma unused(theRefreshArea)
  181.  
  182.     GrafPtr             mySavedPort;
  183.     
  184.     GetPort(&mySavedPort);
  185.     MacSetPort(QTFrame_GetPortFromWindowReference(theWindow));
  186.     
  187.     BeginUpdate(QTFrame_GetWindowFromWindowReference(theWindow));
  188.     //EraseRect(theRefreshArea);        // this is important only for non-rectangular movies
  189.     
  190.     // ***insert application-specific drawing here***
  191.     
  192.     // draw the movie controller and its movie
  193.     MCDoAction(QTFrame_GetMCFromWindow(theWindow), mcActionDraw, theWindow);
  194.     
  195.     EndUpdate(QTFrame_GetWindowFromWindowReference(theWindow));
  196.     MacSetPort(mySavedPort);
  197. }
  198.  
  199.  
  200. //////////
  201. //
  202. // QTApp_HandleContentClick
  203. // Handle mouse button clicks in the specified window.
  204. //
  205. //////////
  206.  
  207. void QTApp_HandleContentClick (WindowReference theWindow, EventRecord *theEvent)
  208. {
  209. #pragma unused(theEvent)
  210.  
  211.     GrafPtr             mySavedPort;
  212.     
  213.     GetPort(&mySavedPort);
  214.     MacSetPort(QTFrame_GetPortFromWindowReference(theWindow));
  215.     
  216.     // ***insert application-specific content-click processing here***
  217.  
  218.     MacSetPort(mySavedPort);
  219. }
  220.  
  221.  
  222. //////////
  223. //
  224. // QTApp_HandleKeyPress
  225. // Handle application-specific key presses.
  226. // Returns true if the key press was handled, false otherwise.
  227. //
  228. //////////
  229.  
  230. Boolean QTApp_HandleKeyPress (char theCharCode)
  231. {
  232.     Boolean        isHandled = true;
  233.     
  234.     switch (theCharCode) {
  235.     
  236.         // ***insert application-specific key-press processing here***
  237.  
  238.         default:
  239.             isHandled = false;
  240.             break;
  241.     }
  242.  
  243.     return(isHandled);
  244. }
  245.  
  246.  
  247. //////////
  248. //
  249. // QTApp_HandleMenu
  250. // Handle selections in the application's menus.
  251. //
  252. // The theMenuItem parameter is a UInt16 version of the Windows "menu item identifier". 
  253. // When called from Windows, theMenuItem is simply the menu item identifier passed to the window procedure.
  254. // When called from MacOS, theMenuItem is constructed like this:
  255. //     *high-order 8 bits == the Macintosh menu ID (1 thru 256)
  256. //     *low-order 8 bits == the Macintosh menu item (sequential from 1 to ordinal of last menu item in menu)
  257. // In this way, we can simplify the menu-handling code. There are, however, some limitations, mainly that
  258. // the menu item identifiers on Windows must be derived from the Mac values. 
  259. //
  260. //////////
  261.  
  262. Boolean QTApp_HandleMenu (UInt16 theMenuItem)
  263. {
  264.     Boolean                myIsHandled = false;            // false => allow caller to process the menu item
  265.     
  266.     switch (theMenuItem) {
  267.     
  268.         case IDM_MAKE_SPRITE_MOVIE:
  269.             QTWired_CreateWiredSpritesMovie();
  270.             myIsHandled = true;
  271.             break;
  272.             
  273.         default:
  274.             break;
  275.             
  276.     } // switch (theMenuItem)
  277.     
  278.     return(myIsHandled);
  279. }
  280.  
  281.  
  282. //////////
  283. //
  284. // QTApp_AdjustMenus
  285. // Adjust state of items in the application's menus.
  286. //
  287. // Currently, the Mac application has only one app-specific menu ("Test"); you could change that.
  288. //
  289. //////////
  290.  
  291. void QTApp_AdjustMenus (WindowReference theWindow, MenuReference theMenu)
  292. {
  293. #pragma unused(theWindow, theMenu)
  294.     
  295. }
  296.  
  297.  
  298. //////////
  299. //
  300. // QTApp_HandleEvent
  301. // Perform any application-specific event loop actions.
  302. //
  303. // Return true to indicate that we've completely handled the event here, false otherwise.
  304. //
  305. //////////
  306.  
  307. Boolean QTApp_HandleEvent (EventRecord *theEvent)
  308. {
  309. #pragma unused(theEvent)
  310.  
  311.     // ***insert application-specific event handling here***
  312.     
  313.     return(false);            // no-op for now
  314. }
  315.  
  316.  
  317. //////////
  318. //
  319. // QTApp_SetupController
  320. // Configure the movie controller.
  321. //
  322. //////////
  323.  
  324. void QTApp_SetupController (MovieController theMC)
  325. {
  326.     long            myControllerFlags;
  327.     
  328.     // CLUT table use
  329.     MCDoAction(theMC, mcActionGetFlags, &myControllerFlags);
  330.     MCDoAction(theMC, mcActionSetFlags, (void *)(myControllerFlags | mcFlagsUseWindowPalette));
  331.  
  332.     // enable keyboard event handling
  333.     MCDoAction(theMC, mcActionSetKeysEnabled, (void *)true);
  334.     
  335.     // disable drag support
  336.     MCDoAction(theMC, mcActionSetDragEnabled, (void *)false);
  337. }
  338.  
  339.  
  340. //////////
  341. //
  342. // QTApp_SetupWindowObject
  343. // Do any application-specific initialization of the window object.
  344. //
  345. //////////
  346.  
  347. void QTApp_SetupWindowObject (WindowObject theWindowObject)
  348. {
  349. #pragma unused(theWindowObject)
  350.  
  351.     // ***insert application-specific window object configuration here***
  352. }
  353.  
  354.  
  355. //////////
  356. //
  357. // QTApp_RemoveWindowObject
  358. // Do any application-specific clean-up of the window object.
  359. //
  360. //////////
  361.  
  362. void QTApp_RemoveWindowObject (WindowObject theWindowObject)
  363. {
  364. #pragma unused(theWindowObject)
  365.     
  366.     // ***insert application-specific window object clean-up here***
  367.  
  368.     // QTFrame_DestroyMovieWindow in MacFramework.c or QTFrame_MovieWndProc in WinFramework.c
  369.     // releases the window object itself
  370. }
  371.  
  372.  
  373. //////////
  374. //
  375. // QTApp_MCActionFilterProc 
  376. // Intercept some actions for the movie controller.
  377. //
  378. // NOTE: The theRefCon parameter is a handle to a window object record.
  379. //
  380. //////////
  381.  
  382. PASCAL_RTN Boolean QTApp_MCActionFilterProc (MovieController theMC, short theAction, void *theParams, long theRefCon)
  383. {
  384. #pragma unused(theMC, theParams)
  385.  
  386.     Boolean                isHandled = false;            // false => allow controller to process the action
  387.     WindowObject        myWindowObject = NULL;
  388.     
  389.     myWindowObject = (WindowObject)theRefCon;
  390.     if (myWindowObject == NULL)
  391.         return(isHandled);
  392.         
  393.     switch (theAction) {
  394.     
  395.         // handle window resizing
  396.         case mcActionControllerSizeChanged:
  397.             if (MCIsControllerAttached(theMC) == 1)
  398.                 QTFrame_SizeWindowToMovie(myWindowObject);
  399.             break;
  400.  
  401.         // handle idle events
  402.         case mcActionIdle:
  403.             QTApp_Idle((**myWindowObject).fWindow);
  404.             break;
  405.             
  406.         default:
  407.             break;
  408.             
  409.     } // switch (theAction)
  410.     
  411.     return(isHandled);    
  412. }
  413.  
  414.  
  415. #if TARGET_OS_MAC
  416. ///////////////////////////////////////////////////////////////////////////////////////////////////////////
  417. //
  418. // Apple Event functions.
  419. //
  420. // Use these functions to install handlers for Apple Events and to handle those events.
  421. //
  422. ///////////////////////////////////////////////////////////////////////////////////////////////////////////
  423.  
  424. //////////
  425. //
  426. // QTApp_InstallAppleEventHandlers 
  427. // Install handlers for Apple Events.
  428. //
  429. //////////
  430.  
  431. void QTApp_InstallAppleEventHandlers (void)
  432. {
  433.     long        myAttrs;
  434.     OSErr        myErr = noErr;
  435.     
  436.     // see whether the Apple Event Manager is available in the present operating environment;
  437.     // if it is, install handlers for the four required Apple Events
  438.     myErr = Gestalt(gestaltAppleEventsAttr, &myAttrs);
  439.     if (myErr == noErr) {
  440.         if (myAttrs & (1L << gestaltAppleEventsPresent)) {
  441.             // create routine descriptors for the Apple event handlers
  442.             gHandleOpenAppAEUPP = NewAEEventHandlerUPP(QTApp_HandleOpenApplicationAppleEvent);
  443.             gHandleOpenDocAEUPP = NewAEEventHandlerUPP(QTApp_HandleOpenDocumentAppleEvent);
  444.             gHandlePrintDocAEUPP = NewAEEventHandlerUPP(QTApp_HandlePrintDocumentAppleEvent);
  445.             gHandleQuitAppAEUPP = NewAEEventHandlerUPP(QTApp_HandleQuitApplicationAppleEvent);
  446.             
  447.             // install the handlers
  448.             AEInstallEventHandler(kCoreEventClass, kAEOpenApplication, gHandleOpenAppAEUPP, 0L, false);
  449.             AEInstallEventHandler(kCoreEventClass, kAEOpenDocuments, gHandleOpenDocAEUPP, 0L, false);
  450.             AEInstallEventHandler(kCoreEventClass, kAEPrintDocuments, gHandlePrintDocAEUPP, 0L, false);
  451.             AEInstallEventHandler(kCoreEventClass, kAEQuitApplication, gHandleQuitAppAEUPP, 0L, false);
  452.         }
  453.     }
  454. }
  455.         
  456.         
  457. //////////
  458. //
  459. // QTApp_HandleOpenApplicationAppleEvent 
  460. // Handle the open-application Apple Events.
  461. //
  462. //////////
  463.  
  464. PASCAL_RTN OSErr QTApp_HandleOpenApplicationAppleEvent (const AppleEvent *theMessage, AppleEvent *theReply, unsigned long theRefcon)            
  465. {
  466. #pragma unused(theMessage, theReply, theRefcon)
  467.     
  468.     // we don't need to do anything special when opening the application
  469.     return(noErr);
  470. }
  471.  
  472.  
  473. //////////
  474. //
  475. // QTApp_HandleOpenDocumentAppleEvent 
  476. // Handle the open-document Apple Events. This is based on Inside Macintosh: IAC, pp. 4-15f.
  477. //
  478. // Here we process an Open Documents AE only for files of type MovieFileType.
  479. //
  480. //////////
  481.  
  482. PASCAL_RTN OSErr QTApp_HandleOpenDocumentAppleEvent (const AppleEvent *theMessage, AppleEvent *theReply, unsigned long theRefcon)            
  483. {
  484. #pragma unused(theReply, theRefcon)
  485.  
  486.     long            myIndex;
  487.     long            myItemsInList;
  488.     AEKeyword        myKeyWd;
  489.     AEDescList          myDocList;
  490.     long            myActualSize;
  491.     DescType        myTypeCode;
  492.     FSSpec            myFSSpec;
  493.     OSErr            myIgnoreErr = noErr;
  494.     OSErr            myErr = noErr;
  495.     
  496.     // get the direct parameter and put it into myDocList
  497.     myDocList.dataHandle = NULL;
  498.     myErr = AEGetParamDesc(theMessage, keyDirectObject, typeAEList, &myDocList);
  499.     
  500.     // count the descriptor records in the list
  501.     if (myErr == noErr)
  502.         myErr = AECountItems(&myDocList, &myItemsInList);
  503.     else
  504.         myItemsInList = 0;
  505.     
  506.     // open each specified file
  507.     for (myIndex = 1; myIndex <= myItemsInList; myIndex++)
  508.         if (myErr == noErr) {
  509.             myErr = AEGetNthPtr(&myDocList, myIndex, typeFSS, &myKeyWd, &myTypeCode, (Ptr)&myFSSpec, sizeof(myFSSpec), &myActualSize);
  510.             if (myErr == noErr) {
  511.                 FInfo        myFinderInfo;
  512.             
  513.                 // verify that the file type is MovieFileType; to do this, get the Finder information
  514.                 myErr = FSpGetFInfo(&myFSSpec, &myFinderInfo);    
  515.                 if (myErr == noErr) {
  516.                     if (myFinderInfo.fdType == MovieFileType)
  517.                         // we've got a movie file; just open it
  518.                         QTFrame_OpenMovieInWindow(NULL, &myFSSpec);
  519.                 }
  520.             }
  521.         }
  522.  
  523.     if (myDocList.dataHandle)
  524.         myIgnoreErr = AEDisposeDesc(&myDocList);
  525.     
  526.     // make sure we open the document in the foreground        
  527.     gAppInForeground = true;
  528.     return(myErr);
  529. }
  530.  
  531.  
  532. //////////
  533. //
  534. // QTApp_HandlePrintDocumentAppleEvent 
  535. // Handle the print-document Apple Events.
  536. //
  537. // NOT YET IMPLEMENTED.
  538. //
  539. //////////
  540.  
  541. PASCAL_RTN OSErr QTApp_HandlePrintDocumentAppleEvent (const AppleEvent *theMessage, AppleEvent *theReply, unsigned long theRefcon)            
  542. {
  543. #pragma unused(theMessage, theReply, theRefcon)
  544.  
  545.     return(errAEEventNotHandled);
  546. }
  547.  
  548.  
  549. //////////
  550. //
  551. // QTApp_HandleQuitApplicationAppleEvent 
  552. // Handle the quit-application Apple Events.
  553. //
  554. //////////
  555.  
  556. PASCAL_RTN OSErr QTApp_HandleQuitApplicationAppleEvent (const AppleEvent *theMessage, AppleEvent *theReply, unsigned long theRefcon)            
  557. {
  558. #pragma unused(theMessage, theReply, theRefcon)
  559.  
  560.     // close down the entire framework and application
  561.     QTFrame_QuitFramework();
  562.     return(noErr);
  563. }
  564. #endif // TARGET_OS_MAC
  565.  
  566.  
  567.